package com.vieewer.btdemo.view.microsoft.translate

import android.content.Intent
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.KeyEvent
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import com.vieewer.btdemo.view.microsoft.audio.TranslateAudioActivity
import com.vieewer.btdemo.wifimain.RadioActivity
import kotlinx.android.synthetic.main.activity_microsoft_translate.*

class TranslateLanguageActivity : BaseActivity() {

    private var mHasFocus: Boolean = false
    private var  mSelectOCR : Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_microsoft_translate)
        setSelectedUI()
    }


    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        mHasFocus = hasFocus
    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        Log.i("yjj", "TravelActivity onKeyUp >>>keyCode" + keyCode + ", action =" + event!!.action)

        when(keyCode) {

            119 -> {  //Fn按键对应键值为119

            }

            22 -> {
                if (mHasFocus) {
                    Log.i("yjj", "TranslateLanguageActivity onKeyUp 22")
                    mSelectOCR = !mSelectOCR
                    setSelectedUI()
                }
            }


            21 -> {
                if (mHasFocus) {
                    Log.i("yjj", "TranslateLanguageActivity onKeyUp 21")
                    mSelectOCR = !mSelectOCR
                    setSelectedUI()
                }

            }

        }

        return super.onKeyUp(keyCode, event)
    }



    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        if (keyCode == 66 && mHasFocus) {
            Log.i("yjj", "TranslateLanguageActivity onKeyDown  66")
            gotoDetail()
        }
        return super.onKeyDown(keyCode, event)

    }

    private fun setSelectedUI() {
        if (mSelectOCR) {
            txt_translate_voice.setTextColor(ContextCompat.getColor(this, R.color.white))
            img_translate_voice.setImageResource(R.drawable.icon_translate_voice_n)
            txt_translate_ocr.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            img_translate_ocr.setImageResource(R.drawable.icon_translate_ocr_h)
        } else {
            txt_translate_voice.setTextColor(ContextCompat.getColor(this, R.color.main_color))
            img_translate_voice.setImageResource(R.drawable.icon_translate_voice_h)
            txt_translate_ocr.setTextColor(ContextCompat.getColor(this, R.color.white))
            img_translate_ocr.setImageResource(R.drawable.icon_translate_ocr_n)
        }

    }


    private fun gotoDetail() {
        if (mSelectOCR) {
            startActivity(Intent(this, TranslateImgActivity::class.java))
        } else {
            startActivity(Intent(this, TranslateAudioActivity::class.java))
        }
    }






}